<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--                        <el-form-item label="所属部门id" prop="deptId">-->
      <!--                            <el-input v-model="queryParams.deptId" placeholder="请输入所属部门id" clearable size="mini"-->
      <!--                                      @keyup.enter.native="handleQuery"/>-->
      <!--                        </el-form-item>-->
      <el-form-item label="所属小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择小区" @change="shaixuan" filterable clearable
                   size="mini">
          <el-option v-for="estate in estatesOptions"
                     :key="estate.id"
                     :label="estate.estateName"
                     :value="estate.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="所属部门" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择所属部门" size="mini">
          <el-option v-for="dept in deptOptions"
                     :key="dept.deptId"
                     :label="dept.deptName"
                     :value="dept.deptId"/>
        </el-select>
      </el-form-item>
      <el-form-item label="发布人" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入发布人姓名" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="查询时间" prop="propertyName">
        <el-date-picker v-model="dateRange" clearable size="mini" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['module:estateLog:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="${comment}" align="center" prop="id" v-if="false"/>
      <el-table-column label="微信用户" align="center" prop="userName"/>
      <el-table-column label="管理员" align="center" prop="sysUserName"/>
      <el-table-column label="所属部门" align="center" prop="deptName"/>
      <el-table-column label="所属小区" align="center" prop="estateName"/>
      <el-table-column label="发布时间" align="center" prop="createTime"/>
      <el-table-column label="发布人姓名" align="center" prop="name"/>
      <!--                    <el-table-column label="日志内容" align="center" prop="content" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:estateLog:edit']">查看日志内容
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <div v-html="content"></div>
    </el-dialog>

  </div>
</template>

<script>
import {listLog, getLog, delLog, addLog, updateLog, exportLog} from "@/api/module/estate/log";
import {listAllEstate} from "@/api/module/estate/estate";
import {getDeptList, listDept} from "@/api/system/dept";

export default {
  name: "Log",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      estatesOptions: [],
      // 工作日志表格数据
      logList: [],
      // 弹出层标题
      title: "日志内容",
      content: undefined,
      // 是否显示弹出层
      open: false,
      dateRange: [],
      deptOptions: [],
      queryParamsDept: {
        parentId: undefined,
        estateId: undefined,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateId: undefined,
        name: undefined,
        beginDate: undefined,
        endDate: undefined,
        deptId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getEstateList();
    this.getList();
    this.getDeptList();
  },
  methods: {
    /** 查询工作日志列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    shaixuan() {
      if (this.queryParams.estateId != undefined) {
        this.queryParamsDept.estateId = this.queryParams.estateId;
      }
      this.getDeptList();
    },
    getDeptList() {
      if (this.queryParams.estateId != undefined) {
        this.queryParamsDept.estateId = this.queryParams.estateId;
      }
      getDeptList(this.queryParamsDept).then(response => {
        this.deptOptions = response.data;
      });
    },
    getEstateList() {
      listAllEstate().then(response => {
        this.estatesOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        sysUserId: undefined,
        deptId: undefined,
        estateId: undefined,
        name: undefined,
        content: undefined,
        readState: undefined,
        createTime: undefined,
        beginDate: undefined,
        endDate: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.dateRange != undefined && this.dateRange.length === 2) {
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = "工作日志内容";
      this.content = row.content;
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateLog(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addLog(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除工作日志编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delLog(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工作日志数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportLog(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
